home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7828 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 27 Feb 1996 11:23:33 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gvlnlINNppj@anvil.ugrad.cs.ubc.ca>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4gsdno$1bg@umbc9.umbc.edu>,
  12. Jonas J. Schlein <schlein@umbc.edu> wrote:
  13. >Abu Wawda <wawda@alcor.usc.edu> wrote:
  14. >|> I'm having trouble understanding what the address of a static array
  15. >|> is.
  16. >
  17. >I'm having trouble understanding why it matters? You almost never use the
  18. >address of an array directly unless doing something tricky with pointers
  19. >or with particular dimensions of a multiple dimensional array.
  20.  
  21. Not necessarily. The address of an array is very useful in abstracting away the
  22. representation of a variable that happens to be represented as an array. This
  23. is nothing tricky.
  24.  
  25. For example, the LibDES encryption library defines a type:
  26.  
  27.     typedef char des_cblock[8];
  28.  
  29. which can hold DES cipherblocks (such as hashed keys). The LibDES routines take
  30. pointers to this type, which effectively hides its representation. The client
  31. can declare a variable of type des_cblock without caring whether it is a
  32. struct, double, long, array or union.  When passing it to a LibDES library
  33. routine, it will take the address. Unless the code is poorly writte, 
  34. the fact that the variable is actually an array (and hence its name is an
  35. expression which collapses into a pointer to the first element of an array and
  36. is not a modifiable lvalue) is never taken advantage of.
  37.  
  38. The benefit is that (1) the compiler can typecheck that what is being
  39. passed is in fact a pointer to an array of 10 chars, as expected by the
  40. prototype---if des_cblock was a pointer to char, you would not be able to have
  41. this sort of check---and (2) the representation of des_cblock could be changed
  42. to something else without affecting well-behaved client code that doesn't
  43. assume that des_cblock objects can be passed by value to internal routines or
  44. can be assigned.
  45. -- 
  46.  
  47.